package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.MenuConstant;
import com.aplink.generic.constant.type.ListViewSelection;
import com.aplink.generic.pojo.controlspec.ListviewSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.xmlparser.MenuItemParser;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class ListViewFormat {
	private static final String AUTO_REFRESH = "autoRefresh";
	private static final String CONTENTDATA = "contentdata";
	private static final String DISPLAYPROP = "displayprop";
	private static final String EMPTY_ICON = "emptyIcon";
	private static final String EMPTY_MESSAGE = "emptyMessage";
	private static final String FOOTER = "footer";
	private static final String HEADER = "header";
	private static final String LINKING_VALUE_PROP = "linkingValueProp";
	private static final String LOAD_ALL = "loadAll";
	private static final String ROWITEM = "rowitem";
	private static final String RPTRDESIGN = "rptrdesign";
	private static final String SELECTION_MODE = "selectionMode";
	private static final String SWIPE_TITLE = "swipeTitle";
	private static final String SWIPE_TO_REFRESH = "swipeRefreshData";
	private static final String SWIPE_TO_REMOVE = "swipeRemoveData";
	private static final String VALUE_PROPERTY = "valueprop";

	public static ListviewSpec parse(final Node node) {
		final ListviewSpec listviewSpec = new ListviewSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node,
			        ListViewFormat.ROWITEM);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setRowItem(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ListViewFormat.RPTRDESIGN);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setRptrdesign(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ListViewFormat.HEADER);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setHeader(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ListViewFormat.FOOTER);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setFooter(value);
			}

			value = XmlPaserUtil.getAttrValue(node,
			        ListViewFormat.SELECTION_MODE);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setListViewSelection(ListViewSelection.get(value));
			}

			value = XmlPaserUtil.getAttrValue(node, ListViewFormat.LOAD_ALL);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setLoadAll(value.equalsIgnoreCase("Y"));
			}

			value = XmlPaserUtil.getAttrValue(node,
			        ListViewFormat.LINKING_VALUE_PROP);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setLinkingValueProp(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ListViewFormat.DISPLAYPROP);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setDisplayProperty(value);
			}
			value = XmlPaserUtil.getAttrValue(node,
			        ListViewFormat.VALUE_PROPERTY);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setValueProp(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ListViewFormat.CONTENTDATA);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setContentDatal(value);
			}
			value = XmlPaserUtil.getAttrValue(node, EMPTY_ICON);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setEmptyIcon(value);
			}
			value = XmlPaserUtil.getAttrValue(node, EMPTY_MESSAGE);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setEmptyMessage(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SWIPE_TO_REFRESH);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setSwipeToRefresh(value.equalsIgnoreCase("Y"));
				listviewSpec.setAutoRefresh(value.equalsIgnoreCase("Y"));
			}

			value = XmlPaserUtil.getAttrValue(node, SWIPE_TITLE);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setSwipeToRefreshTitle(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SWIPE_TO_REMOVE);
			if (!TextUtils.isEmpty(value)) {
				listviewSpec.setSwipeToRemove(value.equalsIgnoreCase("Y"));
			}

			value = XmlPaserUtil.getAttrValue(node, AUTO_REFRESH);
			if (TextUtils.isEmpty(value)) {
				listviewSpec.setAutoRefresh(true);
			} else {
				listviewSpec.setAutoRefresh(value.equalsIgnoreCase("Y"));
			}
		}
		Element elementFormat = (Element) node;
		NodeList nodeListFormat = elementFormat
		        .getElementsByTagName(MenuConstant.SUB_MENU);
		for (int i = 0; i < nodeListFormat.getLength(); i++) {
			final Node nodeItem = nodeListFormat.item(i);
			final MenuItemSpec popupMenuItem = MenuItemParser
			        .parseMenuItemSpec(nodeItem);
			listviewSpec.getContextMenus().add(popupMenuItem);
		}
		return listviewSpec;
	}
}
